package com.tea.room.service.login.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tea.room.dao.LoginDao;
import com.tea.room.model.Login;
import com.tea.room.model.Page;
import com.tea.room.model.Return;
import com.tea.room.service.login.LoginService;
import com.tea.room.service.wechat.impl.PayServiceImpl;
import com.tea.room.util.ConstantUtil;

@Transactional
@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	private LoginDao loginDao;
	
    private static Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);
	
	@Override
	public Return addUser() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Return mdfUser() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Return findUser(String username, String password, String ip) {
		Return rn = new Return();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(username == null || username.equals("")){
			rn.setIsSuccessflag(false);
			rn.setMessage("账号错误！");
			logger.info("账号错误！");
			return rn;
		}
		Login l = new Login();
		l.setUsername(username);
		Login login = loginDao.query(l);
		if(login == null){
			rn.setIsSuccessflag(false);
			rn.setMessage("账户不存在！");
			logger.info("账户不存在！");
			return rn;
		}
		if(ConstantUtil.LOGIN_FLAG0.equals(login.getFlag()) || ( login.getError_no() != null && login.getError_no()> 4)){
			//累计错误次数为五次时，锁定账号
			if(login.getError_no() == 5){
				login.setFlag(ConstantUtil.LOGIN_FLAG0);
				loginDao.update(login);
			}
			rn.setIsSuccessflag(false);
			rn.setMessage("账户已锁定，请联系管理员！");
			logger.info("账户已锁定，请联系管理员！");
			return rn;
		}
		if(password == null || !password.equals(login.getPassword())){
			Integer no = 1;
			if(login.getError_no() != null){
				no = login.getError_no() + no;
			}
			login.setError_no(no);
			rn.setIsSuccessflag(false);
			rn.setMessage("密码错误!累计5次账户将锁定！累计错误次数：" + login.getError_no());
			logger.info("密码错误!累计5次账户将锁定！累计错误次数：" + login.getError_no());
		}else{
			login.setLoginIp(ip);
			try {
                login.setLoginTime(sdf.parse(sdf.format(new Date())));
            } catch (ParseException e) {
                e.printStackTrace();
            }
			login.setError_no(0);
			rn.setIsSuccessflag(true);
		}
		logger.info("登陆成功");
		loginDao.update(login);
		return rn;
	}

    @Override
    public Page findAllUser(Integer currentPage ,Integer pageSize) {
        int beginPage = (currentPage-1)*pageSize;//开始条数
        int endPage = currentPage*pageSize;//结束条数
        List<Login> list = loginDao.queryList(beginPage, endPage);
        for (Login login : list) {
            login.setFlag(login.getFlag().equals("0")?"冻结":"正常");
        }
        Page pages = new Page(list, list.size());
        return pages;
    }

}
